﻿using Foxit.PDF;
using Foxit.PDF.IO;
using Foxit.PDF.PageElements;
using System.Globalization;


namespace Sustainalytics.Reporting.Framework
{
    public class PercentileSlider : PageElement
    {
        public float Left { get; set; }
        public float Top { get; set; }
        public float Width { get; set; }
        public float Heigth { get; set; }
        public float Radius { get; set; }
        public int Value { get; set; }
        public Font ValueFont { get; set; }
        public float ValueFontSize { get; set; }

        private Group Elements()
        {
            const float borderWidth = 0.1f;
            const float margin = 10f;

            var elements = new Group();

            var top = Top + (Heigth / 2) - 0.5f;

            elements.Add(new Line(Left + margin, top, Width - margin, top, 1.0f, RgbColor.Black));
            elements.Add(new Line(Left + margin, top - 5f, Left + margin, top + 5f, 1.0f, RgbColor.Black));
            elements.Add(new Line(Width - margin, top - 5f, Width - margin, top + 5f, 1.0f, RgbColor.Black));

            var unit = (Width - (margin*2) - (Radius*2) - 2)/100;

            var left = Left + margin + (Value * unit) + 1;

            elements.Add(
                new Circle(left + Radius, top, Radius, Colors.YellowBulletColor, Colors.YellowBulletColor, borderWidth, LineStyle.Solid));

            var labelWidth = Radius * 2;
            var lebelHeigth = ValueFontSize + 2f;
            var labelTop = Top + top - (Radius * 2 - ValueFontSize) / 2 + 1f;
            var labelLeft = Left + left;

            elements.Add(new Label(Value.ToString(CultureInfo.InvariantCulture),
                labelLeft, labelTop, labelWidth, lebelHeigth, ValueFont, ValueFontSize, TextAlign.Center, RgbColor.White));

            return elements;
        }

        public override void Draw(PageWriter writer)
        {
            var elements = Elements();

            writer.Write_q_(true);
            elements.Draw(writer);
            writer.Write_Q(true);
        }
    }
}
